// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-11-12 18:34:15
// 生成路径: internal/app/system/logic/order_types.go
// 生成人：zhou
// desc:订单类型
// company:云南奇讯科技有限公司
// ==========================================================================

package orderTypes

import (
	"context"
	"fmt"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/tiger1103/gfast/v3/internal/app/system/model/entity"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/internal/app/system/dao"
	"github.com/tiger1103/gfast/v3/internal/app/system/model"
	"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterOrderTypes(New())
}

func New() service.IOrderTypes {
	return &sOrderTypes{}
}

type sOrderTypes struct{}

func (s *sOrderTypes) List(ctx context.Context, req *model.OrderTypesSearchReq) (listRes *model.OrderTypesSearchRes, err error) {
	listRes = new(model.OrderTypesSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.OrderTypes.Ctx(ctx).WithAll()
		if req.Id != "" {
			m = m.Where(dao.OrderTypes.Columns().Id+" = ?", req.Id)
		}
		if req.Name != "" {
			m = m.Where(dao.OrderTypes.Columns().Name+" like ?", "%"+req.Name+"%")
		}
		if req.Description != "" {
			m = m.Where(dao.OrderTypes.Columns().Description+" = ?", req.Description)
		}
		if req.IsDel != "" {
			m = m.Where(dao.OrderTypes.Columns().IsDel+" = ?", gconv.Int(req.IsDel))
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.OrderTypes.Columns().CreatedAt+" >=? AND "+dao.OrderTypes.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		var res []*model.OrderTypesListRes
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.OrderTypesListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.OrderTypesListRes{
				Id:          v.Id,
				Name:        v.Name,
				Description: v.Description,
				IsDel:       v.IsDel,
				CreatedAt:   v.CreatedAt,
			}
		}
	})
	return
}

func (s *sOrderTypes) GetById(ctx context.Context, id int) (res *model.OrderTypesInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.OrderTypes.Ctx(ctx).WithAll().Where(dao.OrderTypes.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
	})
	return
}

func (s *sOrderTypes) OrderTypeExists(ctx context.Context, typeName string) error {
	orderType := (*entity.OrderTypes)(nil)
	err := g.Try(ctx, func(ctx context.Context) {
		m := dao.OrderTypes.Ctx(ctx)
		m = m.Where(fmt.Sprintf("%s='%s'",
			dao.OrderTypes.Columns().Name,
			typeName))
		err := m.Limit(1).Scan(&orderType)
		liberr.ErrIsNil(ctx, err, "查询订单类型失败")
		if orderType == nil {
			return
		}
		if orderType.Name == typeName {
			liberr.ErrIsNil(ctx, gerror.New("订单类型已存在"))
		}
	})
	return err
}

func (s *sOrderTypes) Add(ctx context.Context, req *model.OrderTypesAddReq) (err error) {
	err = s.OrderTypeExists(ctx, req.Name)
	if err != nil {
		return
	}
	err = g.Try(ctx, func(ctx context.Context) {
		liberr.ErrIsNil(ctx, err)
		_, err = dao.OrderTypes.Ctx(ctx).Insert(do.OrderTypes{
			Name:        req.Name,
			Description: req.Description,
			IsDel:       req.IsDel,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sOrderTypes) Edit(ctx context.Context, req *model.OrderTypesEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.OrderTypes.Ctx(ctx).WherePri(req.Id).Update(do.OrderTypes{
			Name:        req.Name,
			Description: req.Description,
			IsDel:       req.IsDel,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sOrderTypes) Delete(ctx context.Context, ids []int) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.OrderTypes.Ctx(ctx).Delete(dao.OrderTypes.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}
